package io.wjc.service;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

/**
 * 首次从数据库中加载，第二次直接从缓存中读取
 */
@Component
public class SimpleBookRepository implements BookRepository {

  @Override
  @Cacheable(value = "books")
  public Book getByIsbn(String isbn) {
    simulateSlowService();
    return new Book(isbn, "Some book");
  }

  // Don't do this at home
  private void simulateSlowService() {
    try {
      System.out.println("Read from DB....");
      long time = 3000L;
      Thread.sleep(time);
    } catch (InterruptedException e) {
      throw new IllegalStateException(e);
    }
  }

}